﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hack.Generators.RouterKeyGenerator
{
#region WEPKey


    public enum InitialValue { DefaultIntegerValue = -1, DefaultIntegerStartValue = 0 }
    public enum WepKeyType { Byte64 = 5, Byte128 = 13, Byte152 = 16, Byte256 = 29 }

    /// <summary>
    /// Interface IWepKey
    /// </summary>
    public interface IWepKey
    {
        string GenerateStrongKey(int type);
        int LocateAsciiIndex(char Character);
        string StringToHex();
        string GetWepKey { get; set; }
    }

    /// <summary>
    /// Wired Equivalent Privacy
    /// </summary>
    public static class WEPKeyGenerator
    {
        
    }
    /// <summary>
    /// Implementation class for interface IWepKey.
    ///      NOTES 
    ///      If your product vendor requests 40-bit keys, use the 64-bit key\n-> If your product vendor requests 104-bit keys, use the 128-bit key\n 
    ///     -> One ASCII Character is Eight (8) Bits\n-> One HEX Character is Four (4) Bits\n
	///		-> 40 or 64 bit ASCII WEP code has 5 characters\n-> 40 or 64 bit HEX WEP code has 10 characters\n
    ///		-> 128 bit ASCII WEP code has 13 characters\n-> 128 bit HEX WEP code has 26 character
    /// </summary>
    public class WepKey : IWepKey
    {
        /// <remarks>
        /// Two public static array's for serving as a resource
        /// witch will be used to match the input (string) value.
        /// Both array's share the same index.
        /// </remarks>
        /// <summary>
        /// These are the 95 most possible characters from witch 
        /// to choose for generating the WEP key.
        /// </summary>
        public static char[] asciiArray = new char[95] {
	        ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', 
			'*', '+', ',', '-', '.', '/','0', '1', '2', '3', 
			'4', '5', '6', '7', '8', '9', ':', ';', '<', '=', 
			'>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 
			'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 
			'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', 
			'\\', ']', '^', '_', '\'', 'a', 'b', 'c', 'd', 'e', 
			'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
			'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 
			'z', '{', '|', '}', '~'};
        /// <summary>
        /// 95 HEX value's that uses the ASCII index from above.
        /// </summary>
        public static string[] hexArray = new string[95] {
            "20","21","22","23","24","25","26","27","28","29",
            "2A","2B","2C","2D","2E","2F","30","31","32","33",
            "34","35","36","37","38","39","3A","3B","3C","3D",
            "3E","3F","40","41","42","43","44","45","46","47",
            "48","49","4A","4B","4C","4D","4E","4F","50","51",
            "52","53","54","55","56","57","58","59","5A","5B",
			"5C","5D","5E","5F","60","61","62","63","64","65",
            "66","67","68","69","6A","6B","6C","6D","6E","6F",
            "70","71","72","73","74","75","76","77","78","79",
            "7A","7B","7C","7D","7E" };
        #region class members

        private string WepString = string.Empty;

        #endregion

        #region constructor(s)

        public WepKey()
        {
        }

        public WepKey(string AsciiValue)
        {
            this.WepString = AsciiValue;
        }

        #endregion

        #region interface implementations

        /// <remarks>
        /// This method first clears the class string variable 'WepString'; then instatiate an Random object;
        /// Then the speudo random generated integer is used as an index to resolves an character
        /// in Resources.asciiArray. The speudo random generated integer is actually limited to the max length of Resources.asciiArray.
        /// Foreach character resolved in Resources.asciiArray puts the actually result in the class variable WepString.
        /// Finally this method returns the combined character string.
        /// </remarks>
        /// <summary>
        /// (5/13/16/29 bytes for 64/128/152/256-bit WEP
        /// </summary>
        public string GenerateStrongKey(int type)
        {
            this.WepString = string.Empty;
            Random rdm = new Random();

            for (int i = (int)InitialValue.DefaultIntegerStartValue; i < type; i++)
            {
                this.WepString += asciiArray[rdm.Next(asciiArray.Length)];
            }
            return this.StringToHex();
        }

        /// <remarks>
        /// This method first clears the class string variable 'WepString'; then instatiate an Random object;
        /// Then the speudo random generated integer is used as an index to resolves an character
        /// in Resources.asciiArray. The speudo random generated integer is actually limited to the max length of Resources.asciiArray.
        /// Foreach character resolved in Resources.asciiArray puts the actually result in the class variable WepString.
        /// Finally this method returns the combined character string.
        /// </remarks>
        public string GenerateStrongKey(WepKeyType type)
        {
            return GenerateStrongKey((int)type);
        }

        /// <remarks>
        /// This method actually iterates trough the Resources.asciiArray for finding the parameter value Character in the
        /// Resources.asciiArray, and finally returns the ASCII integer index of this particular character.
        /// </remarks>
        public int LocateAsciiIndex(char Character)
        {
            int Result = (int)InitialValue.DefaultIntegerValue;
            try
            {
                for (int i = (int)InitialValue.DefaultIntegerStartValue; i < asciiArray.Length; i++)
                {
                    if (Character == asciiArray[i])
                    {
                        Result = i;
                        break;
                    }
                }
            }
            catch (System.IndexOutOfRangeException ex)
            {
                throw ex;
            }
            return Result;
        }

        /// <remarks>
        /// This method actually iterates trough the length of the passphrase to match each character in
        /// the Resources.asciiArray using the method LocateAciiIndex.
        /// Finally the actually result is matched in the Resources.hexArray and returns the result in 
        /// variable string Result. Finally string Result is returned to the caller.
        /// </remarks>
        public string StringToHex()
        {
            int i = (int)InitialValue.DefaultIntegerValue; int j = i;
            string Result = string.Empty;
            try
            {
                for (i = (int)InitialValue.DefaultIntegerStartValue; i < WepString.Length; i++)
                {
                    j = this.LocateAsciiIndex(WepString[i]);
                    Result += hexArray[j];
                }
            }
            catch (System.IndexOutOfRangeException ex)
            {
                throw ex;
            }
            return Result;
        }

        #region properties
        public string GetWepKey
        {
            get { return this.WepString; }
            set { this.WepString = value; }
        }
        #endregion

        #endregion
    }

#endregion
}
